<?php

!defined('DEBUG') AND exit('Access Denied.');

$action = param(1);

if(!admin_authority_check()) header("Location:".url('index-login'));

if($action == 'pay') {
	//参数
	$status = param('status', -1);	// 订单状态
	$channel = param('channel', 0);	// 通道
	$merchant = param('merchant', '');	// 商户订单号
	$plat = param('plat', '');	// 平台订单号
	$order_username = param('order_username', '');	// 商户帐号
	$fdate = param('fdate', date('Y-m-d H:i:s', strtotime('today')));	// 开始时间
	$tdate = param('tdate', date('Y-m-d H:i:s', strtotime('today')+86399));	// 结束时间
	$money_start = param('money_start', '');	// 起始金额
	$money_end = param('money_end', '');	// 结束金额
	$export = param('export', 0);	// 导出excel
	//条件
	$cond = array();
	if($order_username!='') $cond['username'] = $order_username; 
	if($status!=-1) $cond['status'] = $status;
	if($channel!=0) $cond['chid'] = $channel;
	if($merchant!='') $cond['order_merchant'] = $merchant;
	$cond['time'] = array('>='=>strtotime($fdate),'<='=>strtotime($tdate));
	if($plat!='') $cond['order_plat'] = $plat;
	//金额段
	if($money_start!='' && $money_end!=''){
		$cond['coin'] = array('>='=>$money_start,'<='=>$money_end);
	}elseif($money_start!=''){
		$cond['coin'] = array('>='=>$money_start);
	}elseif($money_end!=''){
		$cond['coin'] = array('<='=>$money_end);
	}
	//导出excel
	if($export){
		$log = db_find('order', $cond, array('time'=>'-1'), 1, 10000);
		if(!empty($log)){
			//colname
			$colname = array('商户帐号','通道','商户单号','平台单号','金额','支付金额','订单状态','通知状态','时间','商户/代理/平台');
			//data
			$data = array();
			foreach($log as $k=>$v){
				$data[$k]['username'] = $v['username'];//商户帐号
				$data[$k]['channel'] = $v['channel'];//通道
				$data[$k]['order_merchant'] = ' '.$v['order_merchant'];//商户单号
				$data[$k]['order_plat'] = ' '.$v['order_plat'];//平台单号
				$data[$k]['coin'] = $v['coin'];//金额
				$data[$k]['coin_pay'] = $v['coin_pay'];//支付金额
				$data[$k]['status'] = ($v['status']==1?'已付款':'未付款');//订单状态
				$data[$k]['notice_status'] = ($v['notice_status']==1?'已通知':'未通知');//通知状态
				$data[$k]['time'] = date('Y-m-d H:i:s', $v['time']);//时间
				$data[$k]['coin_merchant'] = $v['coin_merchant'].' / '.$v['coin_proxy'].' / '.$v['coin_plat'];//商户/代理/平台
			}
			//
			$title = "交易记录 ($fdate-$tdate)";
			$filename = '交易记录';
			export_excel($title,$colname,$data,$filename);//下载excel
		}else{
			exit('没有数据');
		}
	}
	$channels = db_find('channel', array(), array(), 1, 300);	
	//统计
	$total = db_find_one('order', $cond, array(), array('sum(coin) as coin_total,sum(coin_pay) as coin_pay_total,sum(coin_merchant) as coin_merchant,sum(coin_proxy) as coin_proxy,sum(coin_plat) as coin_plat'));
	//
	$page = param('page', 1);
	$pagesize = 100;
	$log = db_find('order', $cond, array('time'=>'-1'), $page, $pagesize);
	$param = "?status=$status&order_username=$order_username&channel=$channel&merchant=$merchant&plat=$plat&fdate=$fdate&tdate=$tdate&money_start=$money_start&money_end=$money_end";
	$pagination = pagination(url("order-pay").$param."&page={page}", db_count('order', $cond), $page, $pagesize);
	$header['title'] = '支付订单 - '.$conf['sitename'];
	include _include(ADMIN_PATH."view/htm/order_pay.htm");

}else if($action == 'redo'){
	$id = param(3);
	$order = order_read($id);
	if(!$order) message(0, '订单无效');
	$merchant = user_read($order['uid']);
	if(!$merchant) message(0, '商户无效');
	$redo = param(2);
	if($redo=='pay'){//支付
		$passcode = param('passcode');
		if(md5(md5($passcode).'ryTY45Rg')!=$conf['passcode']){
			message(0, '安全码错误');
		}
		if($order['status'] != 1){
			$orderstatus = 1;
			$paymoney = $order['coin'];
    		// 更新商户信息
    		$coin_pay = $orderstatus == 1 ? $paymoney : 0;
    		$rate_merchant = $merchant['rate']; //商户的费率
    		$coin_merchant = round($coin_pay * (1 - $rate_merchant), 3); //商户实得
    		user__update($order['uid'], array('coin+' => $coin_merchant, 'tj_pay+' => $paymoney));
    		// 代理奖励
    		$coin_proxy = 0;
    		$rate_proxy = 0;
    		if ($merchant['parent'] != 0) { //该商户有上级
        		$proxy = user_read($merchant['parent']);
        		$rate_proxy = $proxy['rate'];//代理费率
        		$coin_proxy = $paymoney * ($rate_merchant - $rate_proxy);
        		if ($coin_proxy < 0) $coin_proxy = - $coin_proxy;
        		user__update($merchant['parent'], array('coin+' => $coin_proxy));
    		}
    		// 更新订单
    		$coin_plat = $paymoney * $rate_proxy;
    		order_update($id, array('status' => $orderstatus, 'coin_pay' => $coin_pay, 'coin_merchant' => $coin_merchant, 'coin_proxy' => $coin_proxy, 'coin_plat' => $coin_plat));
    		//更新通道的累积收款金额和实时剩余金额
			$money=$paymoney*0.99;
			db_update('channel', array('id'=>$order['chid']), array('money_total+' => $paymoney, 'money+' => $money));			
		}	
	}
	//通知	
	$sign = md5('partner='.$merchant['apino'].'&ordernumber='.$order['order_merchant'].'&orderstatus='.$order['status'].'&paymoney='.$order['coin'].'&subject='.$order['subject'].$merchant['apisecret']);
	$notice_url = $order['callbackurl']. (strpos($order['callbackurl'],'?')===false?'?':'&') .'partner='.$merchant['apino'].'&ordernumber='.$order['order_merchant'].'&orderstatus='.$order['status'].'&paymoney='.$order['coin'].'&subject='.$order['subject'].'&sysnumber='.$order['order_plat'].'&sign='.$sign;
	//_log($notice_url);
	$res = @file_get_contents($notice_url);
	if (trim($res) == 'ok'){
		order_update($id, array('notice_status'=>1));
		message(1, '通知成功');
	}else{
		message(0, '通知失败');	
	}

}else if($action == 'delete'){

	$id = param('id');
	order_delete($id);
	message(1, '删除成功');

}else if($action == 'cash'){

	if($method=='GET'){
		//参数
		$status = param('status', '-1');
		$number = param('number', '');
		$order_username = param('order_username', '');
		$fdate = param('fdate', date('m/d/Y', strtotime('today')));	// 开始时间
		$tdate = param('tdate', date('m/d/Y', strtotime('today')));	// 结束时间
		$export = param('export', 0);	// 导出excel
		//条件
		$cond = array('time'=>array('>='=>strtotime($fdate),'<'=>strtotime($tdate)+86400));
		if($status!=-1) $cond['status'] = $status;
		if($number!='') $cond['number'] = $number;
		if($order_username!='') $cond['username'] = $order_username;
		//导出excel
		if($export){
			$log = db_find('order_cash', $cond, array('time'=>'-1'), 1, 10000);
			if(!empty($log)){
				//colname
				$colname = array('订单号','商户账户','金额','手续费','银行','银行账号','收款人','提现时间','订单状态','提现类型','处理人','处理时间');
				//data
				$data = array();
				foreach($log as $k=>$v){
					$data[$k]['number'] = ' '.$v['number'];//订单号
					$data[$k]['username'] = ' '.$v['username'];//商户账户
					$data[$k]['coin'] = $v['coin'];//金额
					$data[$k]['fee'] = $v['fee'];//手续费
					$data[$k]['bank'] = $v['bank'].($v['is_dai']==1?'(代付)':'');//银行
					$data[$k]['code'] = ' '.$v['code'];//银行账号
					$data[$k]['name'] = ' '.$v['name'];//收款人
					$data[$k]['time'] = date('Y-m-d H:i:s', $v['time']);//提现时间
					$data[$k]['status'] = $v['status'];//订单状态
					//提现类型
					$data[$k]['type'] = '';
					if($v['type']==1) $data[$k]['type'] = '通道:'.$v['type_name'];
					elseif($v['type']==2) $data[$k]['type'] = '银行:'.$v['type_name'];
					//
					$data[$k]['do_username'] = ' '.$v['do_username'];//处理人
					$data[$k]['do_time'] = ($v['do_time']?date('Y-m-d H:i:s', $v['do_time']):'');//处理时间
				}
				//
				$title = "提现记录 ($fdate-$tdate)";
				$filename = '提现记录';
				export_excel($title,$colname,$data,$filename);//下载excel
			}else{
				exit('没有数据');
			}
		}
		
		file_put_contents('../tmp/remind-cash.txt', '0');
		$channels = db_find('channel', array(), array(), 1, 300);
		//统计
		$total = db_find_one('order_cash', $cond, array(), array('sum(coin) as cash_total,sum(fee) as cash_fee'));
		//
		$page = param('page', 1);
		$pagesize = 100;
		$log = db_find('order_cash', $cond, array('time'=>'-1'), $page, $pagesize);
		$param = "?status=".$status.'&number='.$number.'&order_username='.$order_username.'&fdate='.$fdate.'&tdate='.$tdate;
		$pagination = pagination(url("order-cash").$param."&page={page}", db_count('order_cash', $cond), $page, $pagesize);
		$header['title'] = '提现订单 - '.$conf['sitename'];
		include _include(ADMIN_PATH."view/htm/order_cash.htm");
	
	}else{
		
		$status = urldecode(param(2));
		$id = param(3);
		$order = order_cash_read($id);
		if(!$order) message(0, '订单无效');
		if($order['status']!='待审核') message(0, '订单状态错误');
		$order_cash=array();
		$order_cash['status'] = $status;
		$order_cash['do_username'] = $user['username'];
		$order_cash['do_time'] = $time;
		if($status=='已拒绝'){
        	$coin = $order['is_dai']==1 ? $order['coin'] + $order['fee'] : $order['coin'];
        	user__update($order['uid'], array('coin+'=>$coin));
        }elseif($status=='已审核'){
//			$type_channel = param('type_channel');
//			$type_bank = param('type_bank');
//			if($type_channel=='' && $type_bank=='') message(0, '请选择通道或银行');
//			elseif($type_channel!='' && $type_bank!='') message(0, '只能选择通道或银行其中一个');
//			elseif($type_channel!=''){
//				$order_cash['type'] = 1;
//				$order_cash['type_name'] = $type_channel;
//			}elseif($type_bank!=''){
//				$order_cash['type'] = 2;
//				$order_cash['type_name'] = $type_bank;
//			}
			user__update($order['uid'], array('tj_cash+'=>$order['coin']));		 	
		}
		order_cash_update($id, $order_cash);
        json(array('data' => '提现成功', 'status' => true));
	}

}
function _log($log){
	$log=date("Y-m-d H:i:s").' - '.$log;
	$fp=fopen("D:\wwwroot\pay\pay_test\pay_log/".date("Y-m-d").".txt", "a+");
	fwrite($fp,$log."\r\n");
	fclose($fp);
}
?>